import request from "@/utils/request";

const FAMILY_BASE_URL = "/api/v1/family";

// 类型定义
interface FamilyFormData {
  familyId: any;
  pageNum?: number;
  pageSize?: number;
  familyName: string;
}
interface FamilyDetailFormData {
  pageNum: number;
  pageSize: number;
  familyName: string;
  userId: string;
  familyId: string;
}

interface FamilyItem {
  id: string;
  familyName: string;
  originPlace: string;
  totem: string;
  createTime: string;
  updateTime: string;
  isDeleted: number;
  areaCode: string;
  joinStatus: number | null;
  mergerName: string;
}

interface FamilyResult {
  list: FamilyItem[];
  total: number;
}

interface FamilyOption {
  id: string;
  name: string;
  [key: string]: any;
}

interface JoinFamilyData {
  familyId: string;
  memberInfo: string;
  reason: string;
}

interface NearbyFamilyParams extends FamilyFormData {
  latitude?: number;
  longitude?: number;
  userMergerName?: string; // 用户的mergerName参数
}

const FamilyAPI = {
  /*
   *
   * @param queryParams
   * 创建家谱
   * @returns
   */
  createFamily(queryParams: FamilyFormData) {
    return request({
      url: `${FAMILY_BASE_URL}`,
      method: "POST",
      data: queryParams,
    });
  },

  /**
   * 获取我的家谱详情
   */
  getMyDetail(queryParams: FamilyFormData) {
    return request({
      url: `${FAMILY_BASE_URL}/page/me`,
      method: "GET",
      data: queryParams,
    });
  },

  /**
   * 获取家族分页列表
   * @param {Object} queryParams 查询参数
   * @returns {Promise} 用户分页列表
   */
  getPage(queryParams: FamilyFormData): Promise<FamilyResult> {
    console.log(queryParams);
    return request({
      url: `${FAMILY_BASE_URL}/page`,
      method: "GET",
      data: queryParams,
    });
  },
  /**
   * 获取家谱详情
   */
  getDetail(queryParams: FamilyFormData): Promise<FamilyResult> {
    return request({
      url: `${FAMILY_BASE_URL}/detail/${queryParams.familyId}`,
      method: "POST",
    });
  },
  /**
   * 获取选中的家谱详情
   */
  getDetailCheck(queryParams: FamilyDetailFormData): Promise<FamilyResult> {
    return request({
      url: `${FAMILY_BASE_URL}/detail`,
      method: "GET",
      data: queryParams,
    });
  },

  /**
   * 获取家族树形列表
   * @param {Object} queryParams 查询参数
   * @returns {Promise} 家族树形列表
   */
  getList(queryParams: FamilyFormData): Promise<any[]> {
    return request({
      url: `${FAMILY_BASE_URL}`,
      method: "GET",
      data: queryParams,
    });
  },

  /**
   * 获取家族下拉数据源
   * @param {boolean} [onlyParent] 是否只获取父级家族
   * @returns {Promise} 家族下拉数据源
   */
  getOptions(onlyParent?: boolean): Promise<FamilyOption[]> {
    return request({
      url: `${FAMILY_BASE_URL}/options`,
      method: "GET",
      data: { onlyParent },
    });
  },

  /**
   * 获取家族表单数据
   * @param {string} id 家族ID
   * @returns {Promise} 家族表单数据
   */
  getFormData(id: string): Promise<any> {
    return request({
      url: `${FAMILY_BASE_URL}/${id}/form`,
      method: "GET",
    });
  },

  /**
   * 获取家族表单数据
   * @param {string} code 家族编码
   * @returns {Promise} 家族表单数据
   */
  getFormDataByCode(code: string): Promise<any> {
    return request({
      url: `${FAMILY_BASE_URL}/${code}/formByCode`,
      method: "GET",
    });
  },

  /**
   * 添加家族
   * @param {Object} data 家族表单数据
   * @returns {Promise} 请求结果
   */
  create(data: FamilyFormData): Promise<any> {
    return request({
      url: `${FAMILY_BASE_URL}`,
      method: "POST",
      data,
    });
  },

  /**
   * 修改家族
   * @param {string} id 家族ID
   * @param {Object} data 家族表单数据
   * @returns {Promise} 请求结果
   */
  update(id: string, data: FamilyFormData): Promise<any> {
    return request({
      url: `${FAMILY_BASE_URL}/${id}`,
      method: "PUT",
      data,
    });
  },

  /**
   * 删除家族
   * @param {string} id 家族ID
   * @returns {Promise} 请求结果
   */
  deleteByIds(id: string): Promise<any> {
    return request({
      url: `${FAMILY_BASE_URL}/${id}`,
      method: "DELETE",
    });
  },

  /**
   * 加入家族
   * @param {Object} data 加入家族数据
   * @returns {Promise} 请求结果
   */
  join(data: JoinFamilyData): Promise<any> {
    return request({
      url: `${FAMILY_BASE_URL}/joinFamily`,
      method: "POST",
      data,
    });
  },
  /**
   * 获取我的家族分页列表
   * @param {Object} queryParams 查询参数
   * @returns {Promise} 用户分页列表
   */
  getMyPage(queryParams: FamilyFormData) {
    return request({
      url: `${FAMILY_BASE_URL}/page/me`,
      method: "GET",
      data: queryParams,
    });
  },

  /**
   * 获取附近家谱列表
   * @param {Object} queryParams 查询参数，包含经纬度信息
   * @returns {Promise} 附近家谱列表
   */
  getNearbyFamilies(queryParams: NearbyFamilyParams): Promise<FamilyResult> {
    return request({
      url: `${FAMILY_BASE_URL}/nearby`,
      method: "GET",
      data: queryParams,
    });
  },
};

export default FamilyAPI;
